<template>
  <div>
    <!-- 头部 -->
    <van-nav-bar title="编辑昵称" left-text="取消" right-text="确定" @click-left="$emit('close')" @click-right="onClickRight" />
    <!-- 输入框 -->
    <div style="padding: 10px">
      <van-field v-model="localName" rows="2" autosize type="textarea" maxlength="50" placeholder="请输入新昵称" show-word-limit />
    </div>
  </div>
</template>

<script>
import { updateUserName } from '@/api/user.js'
export default {
  name: 'UpdateUser',
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      // message:""
      localName: '',
    }
  },
  methods: {
    async onClickRight() {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0,
      })
      try {
        if (this.localName.trim() === '') return this.$toast('请输入昵称')
        await updateUserName({
          name: this.localName,
        })
        this.$emit('input', this.localName)
        this.$emit('close')
        this.$toast('更新成功')
      } catch (err) {
        console.dir(err)
        if (err.response && err.response.status === 409) {
          return this.$toast('用户名已存在')
        }
        this.$toast('更新失败')
      }
    },
  },
  created() {
    this.localName = this.value
  },
}
</script>

<style lang="" scoped>
</style>